@echo off 
:: run  standard OpenHarmony Operationg System on two qemu machines
:: and connect each other
:: if you want only one machine, please execute run.bat

tasklist | findstr /i "qemu-system-x86_64.exe" 
if %errorlevel% equ 0 (
    echo "Please shutdown the running qemu virtual machines first"
    pause
    exit
) 

:: simulator the x86_64 machine
set cmdstr=qemu-system-x86_64

:: speed up the execution on windows
set cmdstr=%cmdstr% -accel whpx 

:: the simulator machine type
set cmdstr=%cmdstr% -machine q35 

::the cpu and memory specification
set cmdstr=%cmdstr% -smp 6 -m 4096M 

::boot from disk
set cmdstr=%cmdstr% -boot c 

::boot with no graphics
set cmdstr=%cmdstr% -nographic 

::with no vga card
set cmdstr=%cmdstr% -vga none 

::simulator a gpu card , with a fixed resolution  360X720
set cmdstr=%cmdstr% -device virtio-gpu-pci,xres=360,yres=720 

::simple display with no opengl
set cmdstr=%cmdstr% -display sdl,gl=off 

::sync the time with Windows
set cmdstr=%cmdstr% -rtc base=utc,clock=host 

::simulator a sound card
set cmdstr=%cmdstr% -device es1370 

:: the disk files store somewhere
:: fix it 
:: OHOS_IMG=   or  OHOS_IMG=D:\path1\   or OHOS_IMG=..\path2\   or  OHOS_IMG=path3\path4\
set OHOS_IMG=

:: machines can not share ramdisk, so copy it.
set ramdisk=%OHOS_IMG%ramdisk.img
set ramdisk1=%OHOS_IMG%ramdisk1.img
set ramdisk2=%OHOS_IMG%ramdisk2.img
IF NOT EXIST %ramdisk1% (
    echo copy %ramdisk% to %ramdisk1% ...
    copy %ramdisk% %ramdisk1%
    )
IF NOT EXIST %ramdisk2% (
    echo copy %ramdisk% to %ramdisk2% ...
    copy %ramdisk% %ramdisk2%
    )

::machines should share bzImage
set cmdstr=%cmdstr% -kernel %OHOS_IMG%bzImage

::use qcow2 for reduce disk usage
IF NOT EXIST %OHOS_IMG%updater.qcow2 (
    echo convert updater.img to updater.qcow2 ...
    qemu-img convert -f raw -O qcow2 -c %OHOS_IMG%updater.img %OHOS_IMG%updater.qcow2       
    )
IF NOT EXIST %OHOS_IMG%system.qcow2 (
    echo convert system.img to system.qcow2 ...
    qemu-img convert -f raw -O qcow2 -c %OHOS_IMG%system.img %OHOS_IMG%system.qcow2
    )
IF NOT EXIST %OHOS_IMG%vendor.qcow2 (
    echo convert vendor.img to vendor.qcow2 ...
    qemu-img convert -f raw -O qcow2 -c %OHOS_IMG%vendor.img %OHOS_IMG%vendor.qcow2    
    )
IF NOT EXIST %OHOS_IMG%sys_prod.qcow2 (
    echo convert sys_prod.img to sys_prod.qcow2 ...
    qemu-img convert -f raw -O qcow2 -c %OHOS_IMG%sys_prod.img %OHOS_IMG%sys_prod.qcow2
    )
IF NOT EXIST %OHOS_IMG%chip_prod.qcow2 (
    echo convert chip_prod.img to chip_prod.qcow2 ...
    qemu-img convert -f raw -O qcow2 -c %OHOS_IMG%chip_prod.img %OHOS_IMG%chip_prod.qcow2
    )
IF NOT EXIST %OHOS_IMG%userdata.qcow2 (
    echo convert userdata.img to userdata.qcow2 ...
    qemu-img convert -f raw -O qcow2 -c %OHOS_IMG%userdata.img %OHOS_IMG%userdata.qcow2
    )

::machines can not share the same qcow2 file,  so create a overlay file for each machine.
IF NOT EXIST %OHOS_IMG%updater1.qcow2 (
    echo create updater1.qcow2 and updater2.qcow2 ...    
    qemu-img create -f qcow2 -b %OHOS_IMG%updater.qcow2 -F qcow2 %OHOS_IMG%updater1.qcow2 
    qemu-img create -f qcow2 -b %OHOS_IMG%updater.qcow2 -F qcow2 %OHOS_IMG%updater2.qcow2 
    )
IF NOT EXIST %OHOS_IMG%system1.qcow2 (
    echo create system1.qcow2 and system2.qcow2 ...   
    qemu-img create -f qcow2 -b %OHOS_IMG%system.qcow2  -F qcow2 %OHOS_IMG%system1.qcow2 
    qemu-img create -f qcow2 -b %OHOS_IMG%system.qcow2  -F qcow2 %OHOS_IMG%system2.qcow2 
    )
IF NOT EXIST %OHOS_IMG%vendor1.qcow2 (
    echo create vendor1.qcow2 and vendor2.qcow2 ...   
    qemu-img create -f qcow2 -b %OHOS_IMG%vendor.qcow2  -F qcow2 %OHOS_IMG%vendor1.qcow2 
    qemu-img create -f qcow2 -b %OHOS_IMG%vendor.qcow2  -F qcow2 %OHOS_IMG%vendor2.qcow2 
    system
    )
IF NOT EXIST %OHOS_IMG%sys_prod1.qcow2 (
    echo create sys_prod1.qcow2 and sys_prod2.qcow2 ...   
    qemu-img create -f qcow2 -b %OHOS_IMG%sys_prod.qcow2  -F qcow2 %OHOS_IMG%sys_prod1.qcow2 
    qemu-img create -f qcow2 -b %OHOS_IMG%sys_prod.qcow2  -F qcow2 %OHOS_IMG%sys_prod2.qcow2 
    )
IF NOT EXIST %OHOS_IMG%chip_prod1.qcow2 (
    echo create chip_prod1.qcow2 and chip_prod2.qcow2 ...   
    qemu-img create -f qcow2 -b %OHOS_IMG%chip_prod.qcow2 -F qcow2 %OHOS_IMG%chip_prod1.qcow2 
    qemu-img create -f qcow2 -b %OHOS_IMG%chip_prod.qcow2 -F qcow2 %OHOS_IMG%chip_prod2.qcow2 
    )
IF NOT EXIST %OHOS_IMG%userdata1.qcow2 (
    echo create userdata1.qcow2 and userdata2.qcow2 ...   
    qemu-img create -f qcow2 -b %OHOS_IMG%userdata.qcow2 -F qcow2 %OHOS_IMG%userdata1.qcow2 
    qemu-img create -f qcow2 -b %OHOS_IMG%userdata.qcow2 -F qcow2 %OHOS_IMG%userdata2.qcow2 
    )

::the first machine disk images
set drivestr1=-drive if=virtio,media=disk,format=qcow2,index=0,file=%OHOS_IMG%updater1.qcow2 
set drivestr1=%drivestr1% -drive if=virtio,media=disk,format=qcow2,index=1,file=%OHOS_IMG%system1.qcow2, 
set drivestr1=%drivestr1% -drive if=virtio,media=disk,format=qcow2,index=2,file=%OHOS_IMG%vendor1.qcow2, 
set drivestr1=%drivestr1% -drive if=virtio,media=disk,format=qcow2,index=3,file=%OHOS_IMG%sys_prod1.qcow2, 
set drivestr1=%drivestr1% -drive if=virtio,media=disk,format=qcow2,index=4,file=%OHOS_IMG%chip_prod1.qcow2 
set drivestr1=%drivestr1% -drive if=virtio,media=disk,format=qcow2,index=5,file=%OHOS_IMG%userdata1.qcow2 

::the second machine disk images
set drivestr2=-drive if=virtio,media=disk,format=qcow2,index=0,file=%OHOS_IMG%updater2.qcow2 
set drivestr2=%drivestr2% -drive if=virtio,media=disk,format=qcow2,index=1,file=%OHOS_IMG%system2.qcow2, 
set drivestr2=%drivestr2% -drive if=virtio,media=disk,format=qcow2,index=2,file=%OHOS_IMG%vendor2.qcow2, 
set drivestr2=%drivestr2% -drive if=virtio,media=disk,format=qcow2,index=3,file=%OHOS_IMG%sys_prod2.qcow2, 
set drivestr2=%drivestr2% -drive if=virtio,media=disk,format=qcow2,index=4,file=%OHOS_IMG%chip_prod2.qcow2 
set drivestr2=%drivestr2% -drive if=virtio,media=disk,format=qcow2,index=5,file=%OHOS_IMG%userdata2.qcow2 


::boot parameters for kernel
set bootstr=loglevel=4
set bootstr=%bootstr% console=ttyS0,115200 
set bootstr=%bootstr% init=init 
set bootstr=%bootstr% root=/dev/ram0 rw  
set bootstr=%bootstr% ohos.boot.hardware=virt 
set bootstr=%bootstr% default_boot_device=10007000.virtio_mmio 
set bootstr=%bootstr% ohos.required_mount.system=/dev/block/vdb@/usr@ext4@ro,barrier=1@wait,required 
set bootstr=%bootstr% ohos.required_mount.vendor=/dev/block/vdc@/vendor@ext4@ro,barrier=1@wait,required


::simulator ethernet card, connect the two device on an virtual link
::the first device eth0
set netstr10=-netdev socket,id=net0,connect=127.0.0.1:1234 -device e1000,netdev=net0,mac=52:54:00:12:34:11
::the seond device eth0
set netstr20=-netdev socket,id=net0,listen=:1234 -device e1000,netdev=net0,mac=52:54:00:12:34:22


::the first device eth1,   for  access internet.   also can be connected by cmd "hdc tconn 127.0.0.1:55556"
set netstr11=-netdev user,id=net1,hostfwd=tcp::55556-:55555 -device e1000,netdev=net1
::the second device eth1, for access internet.  also can be connected by cmd "hdc tconn 127.0.0.1:55557"
set netstr21=-netdev user,id=net1,hostfwd=tcp::55557-:55555 -device e1000,netdev=net1


::serial number for the devices
set snstr1=sn=8823456711
set snstr2=sn=8823456722

set bootstr1=%bootstr% %snstr1%
set bootstr2=%bootstr% %snstr2%


set cmdstr1=%cmdstr% -initrd %ramdisk1% %drivestr1% -name OpenHarmony_first
set cmdstr1=%cmdstr1% %netstr10% %netstr11%
set cmdstr1=%cmdstr1% -append 
set cmdstr1=%cmdstr1%^"%bootstr1%^"

set cmdstr2=%cmdstr% -initrd %ramdisk2% %drivestr2% -name OpenHarmony_second
set cmdstr2=%cmdstr2% %netstr20% %netstr21%
set cmdstr2=%cmdstr2% -append 
set cmdstr2=%cmdstr2%^"%bootstr2%^"


::start the second machine , wait for the first machine connect
start "OpenHarmony-second" cmd /k "%cmdstr2%"  

timeout /T 5

::start the first machine,  connect to the second machine
start "OpenHarmony-first" cmd /k "%cmdstr1%" 




